﻿using Media.Hepler;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Filters;
using System.Web.Http.Controllers;
using System.Threading;

namespace Media.Controllers
{
    public class MediaFilter : ActionFilterAttribute//, IExceptionFilter
    {
        private Stopwatch stopwatch = new Stopwatch();

        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            //check domain 
            string strAllowHost = TN_CMSMediaConfig.GetSettingByKey("Media.Domain");
            string[] allowHost = null;
            if (strAllowHost != null)
            {
                allowHost = strAllowHost.Split(',');
            }

            foreach (var host in allowHost)
            {
                if (host == actionContext.Request.RequestUri.Host)
                {
                    return;
                }
            }

            var response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Forbidden access");
            actionContext.Response = response;

        }

        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {

        }
    }
}
